package com.wst.system.util;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public class AESFileUtil {

    private static final String ALGORITHM = "AES";

    public static SecretKey generateAesKey() {
        try {
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128); // 生成128位的AES密钥
            return keyGen.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    Key key;
    public AESFileUtil(String str) {
        getKey(str);//生成密匙
    }
    /**
     * 根据参数生成KEY
     */
    public void getKey(String strKey) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            _generator.init(new SecureRandom(strKey.getBytes()));
            this.key = _generator.generateKey();
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        }
    }
    /**
     * 文件流加密
     * @param file
     * @return
     * @throws Exception
     */
    public InputStream encrypt(InputStream file) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, this.key);
        CipherInputStream cis = new CipherInputStream(file, cipher);
        return cis;
    }
    /**
     * 文件流解密
     * @param file
     * @return
     * @throws Exception
     */
    public InputStream decrypt(InputStream file) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, this.key);
        CipherInputStream cis = new CipherInputStream(file, cipher);
        return cis;
    }
    /**
     * 文件加密
     * @param file
     * @param destFile
     * @throws Exception
     */
    public void encrypt(String file, String destFile) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, this.key);
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(destFile);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = cis.read(buffer)) > 0) {
            out.write(buffer, 0, r);
        }
        cis.close();
        is.close();
        out.close();
    }

    //文件加密
    public byte[] encrypt(byte[] bytes) throws Exception {
        InputStream is=null;
        ByteArrayOutputStream baos=null;
        CipherInputStream cis=null;
        try {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, this.key);
            is= new ByteArrayInputStream(bytes);;
            baos = new ByteArrayOutputStream();
            byte buff[] = new byte[1024];
            int len = 0;
            cis = new CipherInputStream(is, cipher);
            while ((len = cis.read(buff)) > 0) {
                baos.write(buff, 0, len);
            }
            baos.flush();
            return baos.toByteArray();
        }finally {
            if(is!=null) {
                try {
                    is.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null) {
                try {
                    baos.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(cis!=null) {
                try {
                    cis.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //文件解密
    public byte[] decrypt(String file) throws Exception {
        InputStream is=null;
        ByteArrayOutputStream baos=null;
        CipherOutputStream cos=null;
        try {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, this.key);
            is = new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            cos= new CipherOutputStream(baos, cipher);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = is.read(buffer)) >= 0) {
                System.out.println();
                cos.write(buffer, 0, r);
            }
            baos.flush();
            return baos.toByteArray();
        } finally {
            if(is!=null) {
                try {
                    is.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null) {
                try {
                    baos.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(cos!=null) {
                try {
                    cos.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 文件解密
     * @param file
     * @param dest
     * @throws Exception
     */
    public void decrypt(String file, String dest) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, this.key);
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(dest);
        CipherOutputStream cos = new CipherOutputStream(out, cipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = is.read(buffer)) >= 0) {
            System.out.println();
            cos.write(buffer, 0, r);
        }
        cos.close();
        out.close();
        is.close();
    }

    public static void main(String[] args) throws Exception {
        AESFileUtil fileDESUtil = new AESFileUtil("970910");//密钥
       // fileDESUtil.encrypt("D:\\upload\\640.jpg", "D:\\upload\\640_01.jpg"); //加密
        fileDESUtil.decrypt("D:\\upload\\640_01.jpg", "D:\\upload\\640_02.jpg"); //解密
    }
}
